home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The X-Philes (2nd Revision)
/
The X-Philes Number 1 (1995).iso
/
xphiles
/
hp48_2
/
grob2ps
< prev
next >
Wrap
Text File
|
1995-03-31
|
10KB
|
404 lines
Path: seq!spell
From: Johan Pensar RT <jpensar@ra.abo.fi>
Subject: v01i032: grob2ps - grob2ps translator v1.0, Part01/01
Newsgroups: comp.sources.hp48
Followup-To: comp.sys.hp48
Approved: spell@seq.uncwil.edu
Checksum: 182002788 (verify with brik -cv)
Submitted-by: Johan Pensar RT <jpensar@ra.abo.fi>
Posting-number: Volume 1, Issue 32
Archive-name: grob2ps/part01
[ If anyone wants a copy of this program compiled for the PC let
me know and i'll post a copy. Send your default settings. -Chris ]
BEGIN_RDME grob2ps.rdm
For all of you working on workstations or PC's connected to PostScript
laserwriters, here is a program converting a hp48 GROB to PostScript.
I know of programs for conversion of GROB's to epson, portable bitmap and
tiff formats but no program converting directly to PostScript, maybe I
have missed it?
Anyway I wrote this program because I needed to document some programs,
and the documents should include some screendumps. Because i use TeX, the
only possible format was PostScript.
The program is written in C and compiles on my SUN Sparcstation using GNU C,
and also on PC's using Microsoft C.
US users have to change the PAPERWIDTH and PAPERHEIGHT #defines to fit their
papersize.
END_RDME
BEGIN_DOC grob2ps.doc
Program: grob2ps, a hp48 GROB to PostScript translator
Author: Johan Pensar, (c) 1991, may be freely copied as long as all
copyright notices are preserved.
Version: 1.0, 03.09.91
Compiler: Microsoft C or GNU C
Usage:
For a description on how to save and transfer a GROB to another computer,
see the documentation for the grob2tif program on the HP 82208 A Serial
Interface Kit Disk.
This program, grob2ps, works in a similar way to the grob2tif program
on the hp disk, but with some extentions.
If a sourcefilename is supplied, the program reads the file instead of
stdin. If a destinationfilename is supplied, the program opens it instead
of stdout.
Default format is landscape scaled to fit the paper with 1 inch margins,
but the switch -p switches to portrait format. For inclusion into documents,
the switch -e is provided, that outputs a EPS file with origin at 0,0.
When using the EPS format, the switch -s### is provided to change the scale
of the picture, the default scale is 2.0, giving a 93 mm wide picture
for a 131 pixels wide GROB.
For laserwriters connected directly to a serial port, the switch -d inserts
the ^D at the start and end of the document.
Examples:
Printing the file graph.grb in landscape format on a UNIX box
grob2ps graph.grb | lpr -Plaser
Printing in portrait format to a directly connected laserwriter on COM1
grob2ps -p -d graph.grb > COM1
Converting the file to encapsulated postscript for a document,
enlarging it a bit
grob2ps -e -s3.0 graph.grb graph.ps
Getting help
grob2ps -h
Bugs:
Report eventual bugs to jpensar@aton.abo.fi
END_DOC
BEGIN_SRC grob2ps.c
/*
* grob2ps, a program for converting a hp48 GROB (GRaphic OBject)
* to PostScript.
*
* Author: Johan Pensar (c) 1991
* This program may be distributed freely as long as all
* copyright notices are preserved.
* Bugreports to jpensar@aton.abo.fi
*
* Version: 1.0, 03.09.91
*/
/*#define MSC /* if compiling on MesS-DOS */
#include <stdio.h>
#include <ctype.h>
#ifdef MSC
# include <time.h>
#else
# include <sys/types.h>
# include <sys/time.h>
#endif
#define PORTRAIT 0
#define LANDSCAPE 1
/* A4 size in points */
#define PAPERWIDTH 598
#define PAPERHEIGHT 845
/* default scale for encapsulated mode */
#define DEF_SCALE 2.0
/* default orientation in normal mode */
#define DEF_ORIENT LANDSCAPE
int orientation = DEF_ORIENT;
int eps = 0;
double scale = DEF_SCALE;
int direct = 0;
int xsize,ysize;
double atof(char *);
/*
* now() returns current time&date as a string
*/
char * now()
{
time_t date;
if (time(&date) == -1){
perror("grob2ps: time:");
exit(1);
}
return(ctime(&date));
}
/*
* parse_grob() reads the grob header
*/
void parse_grob()
{
int chr;
while( (chr=getchar()) != EOF ) /* The first line starting with %%HP */
if (chr == 10)
break;
if (chr == EOF) {
fprintf(stderr,"grob2ps: illegal grob header!!\n");
exit(1);
}
/* The second line starting with */
/* GROB xsize ysize */
if (scanf("GROB %d %d",&xsize,&ysize) != 2) {
fprintf(stderr,"grob2ps: illegal grob header!\n");
exit(1);
}
}
/*
* print_header() prints the PostScript header
*/
void print_header(file)
char * file;
{
double xs,ys;
int xextra = 0,
yextra = 0;
printf("%%!PS-Adobe-2.0 %s\n",(eps)?"EPSF-1.2":"");
printf("%%%%Creator: %s and grob2ps (hp48 GROB to PostScript translator)\n",
#ifdef MSC
"PC user"
#else
cuserid((char*) NULL)
#endif
);
printf("%%%%Title: grob2ps %s (%d*%d)\n",
(*file!=(char)NULL)?file:"(stdin)",
xsize,ysize);
printf("%%%%CreationDate: %s",now());
if (eps)
printf("%%%%BoundingBox: 0 0 %d %d\n",(int)((double)xsize*scale),
(int)((double)ysize*scale));
printf("%%%%EndComments\n");
printf("/grobimage {\n");
printf(" /picstr width 8 div ceiling cvi string def\n\n");
printf(" {1 exch sub} settransfer\n\n");
printf(" width height 1 [1 0 0 -1 0 height]\n");
printf(" {currentfile picstr readhexstring pop\n");
printf(" 0 1 width 8 div floor cvi {\n");
printf(" dup picstr exch get\n");
printf(" dup 1 and 3 bitshift\n");
printf(" exch dup 2 and 1 bitshift\n");
printf(" exch dup 4 and -1 bitshift\n");
printf(" exch dup 8 and -3 bitshift\n\n");
printf(" exch dup 16 and 3 bitshift\n");
printf(" exch dup 32 and 1 bitshift\n");
printf(" exch dup 64 and -1 bitshift\n");
printf(" exch 128 and -3 bitshift\n");
printf(" or or or or or or or\n");
printf(" picstr 3 1 roll put\n");
printf(" } for\n");
printf(" } image\n");
printf("} def\n\n");
printf("%%%%EndProlog\n");
printf("\ngsave\n\n");
if (!eps) {
if (orientation == LANDSCAPE) {
xs = ((double)PAPERHEIGHT-144.)/(double)xsize;
ys = ((double)PAPERWIDTH-144.)/(double)ysize;
if (xs<ys) {
scale = xs;
xextra= ((double)PAPERWIDTH-144.) - (scale*(double)ysize);
}
else {
scale = ys;
yextra= ((double)PAPERHEIGHT-144.) - (scale*(double)xsize);
}
}
else {
xs = ((double)PAPERWIDTH-144.)/(double)xsize;
ys = ((double)PAPERHEIGHT-144.)/(double)ysize;
if (xs<ys) {
scale = xs;
yextra= ((double)PAPERHEIGHT-144.) - (scale*(double)ysize);
}
else {
scale = ys;
xextra= ((double)PAPERWIDTH-144.) - (scale*(double)xsize);
}
}
printf("%d %d translate %s\n",
((orientation==LANDSCAPE)?
PAPERWIDTH-(72+xextra/2):
72+xextra/2),
72+yextra/2,
(orientation==LANDSCAPE)?"90 rotate":"");
printf("%lf %lf scale\n\n",scale,scale);
}
else
printf("%lf %lf scale\n\n",scale,scale);
printf("/width %d def\n/height %d def\n\n",xsize,ysize);
}
/*
* print_hexdat() prints the hexadecimal data, inserting linefeeds
* every 78'th character to avoid to long lines
*/
void print_hexdat()
{
int chr,chrs=-1;
printf("grobimage\n");
while ((chr=getchar()) != EOF)
if (isalnum(chr)) {
if (!(++chrs%78))
printf("\n");
putchar(chr);
}
}
/*
* print_trailer() prints the PostScript trailer
*/
void print_trailer()
{
printf("\n\ngrestore\n\n");
printf("%%%%Trailer\n");
if (!eps)
printf("showpage\n");
}
/*
* ctrl_d() prints a ^D as end of job
*/
void ctrl_d()
{
printf("\n%c\n",4);
}
main(argc,argv)
int argc;
char **argv;
{
static char file[256] = "";
static char dest[256] = "";
/* parse switches */
while (*(++argv)) {
if (**argv == '-') {
switch (*(*argv+1)) {
case 'l': orientation = LANDSCAPE;
break;
case 'p': orientation = PORTRAIT;
break;
case 'e': eps = 1;
break;
case '?':
case 'h':
fprintf(stderr,
"\n\
grob2ps, a program for converting hp48 GROB to PostScript\n\
\n\
Author: Johan Pensar (jpensar@aton.abo.fi) (c) 1991\n\
This program may be freely distributed as long as this\n\
copyright notice is preserved. (v 1.0)\n\
\n\
Usage: grob2ps [-l [-d]|-p [-d]|-e [-s###]] [sourcefile[destinationfile]]\n\
\n\
-l - landscape mode\n\
-p - portrait mode\n\
-d - direct connection, insert ^D\n\
-e - encapsulated postscript mode\n\
-s - scale (default = %3.1lf)\n\
"
,DEF_SCALE);
exit(0);
case 's': scale = atof( (char*) (*argv+2) );
if (scale <= 0.0)
scale = DEF_SCALE;
break;
case 'd':
direct = 1;
break;
default: fprintf(stderr,"grob2ps: unknown switch '%s'\n",*argv);
}
}
else {
if (*file == (char) NULL)
strcpy(file,*argv);
else
strcpy(dest,*argv);
}
}
/* open files */
if (*file != (char) NULL) {
if (freopen(file,"r",stdin) == (FILE*) NULL) {
fprintf(stderr,"grob2ps: cant open grobfile %s\n",file);
exit(1);
}
}
if (*dest != (char) NULL) {
if (freopen(dest,"w",stdout) == (FILE*) NULL) {
fprintf(stderr,"grob2ps: cant open ps-file %s\n",dest);
exit(1);
}
}
if ((!eps) && (direct))
ctrl_d();
parse_grob();
print_header(file);
print_hexdat();
print_trailer();
if ((!eps) && (direct))
ctrl_d();
exit(0);
}
END_SRC
------------------------------------------------------------------------------
Johan Pensar Email: jpensar@aton.abo.fi
Process Control Laboratory
Abo Akademi University
FINLAND